package com.machine.dao.user.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.machine.bean.Componentpart;
import com.machine.bean.Model;
import com.machine.dao.user.ComponentDao;

@Component(value = "componentDao")
@Transactional
public class ComponentDaoImpl implements ComponentDao {

	private SessionFactory sessionfactory;

	public SessionFactory getSessionfactory() {
		return sessionfactory;
	}

	@Resource
	public void setSessionfactory(SessionFactory sessionfactory) {
		this.sessionfactory = sessionfactory;
	}

	@Override
	public Integer add(com.machine.bean.Componentpart component) {
		return (Integer)sessionfactory.getCurrentSession().save(component);
		//System.out.println("ID : " + component.getId());
	}

	@Override
	public int delete(com.machine.bean.Componentpart component) {
		sessionfactory.getCurrentSession().delete(component);
		return 1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<com.machine.bean.Componentpart> find(String name,int offset, int length) {
		String hql = "from Componentpart componentpart where componentpart.name like '%"
				+ name + "%' order by componentpart.price";
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setFirstResult(offset);  
        query.setMaxResults(length); 
		List<com.machine.bean.Componentpart> comList = query.list();
		return comList;
	}
	
	@Override
	public List<Model> findModel(String name,int offset, int length) {
		String hql = "from Model model where model.name like '%"
			+ name + "%'";
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setFirstResult(offset);  
        query.setMaxResults(length); 
        List<Model> list = query.list();
		return list;
	}
	
	
	
	@Override
	public List<Componentpart> findByTag(String tag,String keywords) {
		int tagNum = Integer.valueOf(tag);
		if(tagNum%100==0){
			String hql = "from Componentpart componentpart where componentpart.name like '%"
					+ keywords + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100";
			
			Session session = sessionfactory.getCurrentSession();
			Query query = session.createQuery(hql);
			List<com.machine.bean.Componentpart> comList = query.list();
			return comList;
		}else{
			String hql = "from Componentpart componentpart where componentpart.name like '%"
					+ keywords + "%' and componentpart.componentnumber-"+tag+"=0";
			
			Session session = sessionfactory.getCurrentSession();
			Query query = session.createQuery(hql);
			List<com.machine.bean.Componentpart> comList = query.list();
			return comList;
		}
	}
	
	@Override
	public List<Componentpart> findByPrice(String tag, String keywords,
			String order) {
		String hql="";
		if(tag.equals("")){
			 if(order.equals("0")){
				 hql = "from Componentpart componentpart where componentpart.name like '%"
							+ keywords + "%' order by componentpart.price asc";
			 }else{
				 hql = "from Componentpart componentpart where componentpart.name like '%"
							+ keywords + "%' order by componentpart.price desc";
			 }
		}else{
			if(order.equals("0")){
				hql = "from Componentpart componentpart where componentpart.name like '%"
						+ keywords + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.price asc";
			}else{
				hql = "from Componentpart componentpart where componentpart.name like '%"
						+ keywords + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.price desc";
			}
		}
		
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<com.machine.bean.Componentpart> comList = query.list();
		return comList;
		
	}
	
	
	@Override
	public List<Componentpart> findBySell(String tag, String keywords,
			String order) {
		String hql="";
		if(tag.equals("")){
			 if(order.equals("0")){
				 hql = "from Componentpart componentpart where componentpart.name like '%"
							+ keywords + "%' order by componentpart.sellnum asc";
			 }else{
				 hql = "from Componentpart componentpart where componentpart.name like '%"
							+ keywords + "%' order by componentpart.sellnum desc";
			 }
		}else{
			if(order.equals("0")){
				hql = "from Componentpart componentpart where componentpart.name like '%"
						+ keywords + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.sellnum asc";
			}else{
				hql = "from Componentpart componentpart where componentpart.name like '%"
						+ keywords + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.sellnum desc";
			}
		}
		
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<com.machine.bean.Componentpart> comList = query.list();
		return comList;
	}
	
	@Override
	public List<Componentpart> findByPro(String tag, String keywords,
			String order) {
		String hql="";
		if(tag.equals("")){
			 if(order.equals("0")){
				 hql = "from Componentpart componentpart where componentpart.name like '%"
							+ keywords + "%' order by componentpart.stockamount asc";
			 }else{
				 hql = "from Componentpart componentpart where componentpart.name like '%"
							+ keywords + "%' order by componentpart.stockamount desc";
			 }
		}else{
			if(order.equals("0")){
				hql = "from Componentpart componentpart where componentpart.name like '%"
						+ keywords + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.stockamount asc";
			}else{
				hql = "from Componentpart componentpart where componentpart.name like '%"
						+ keywords + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.stockamount desc";
			}
		}
		
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<com.machine.bean.Componentpart> comList = query.list();
		return comList;
	}
	
	
	@Override
	public List<Componentpart> findByTime(String tag, String keywords,
			String order) {
		String hql="";
		if(tag.equals("")){
			 if(order.equals("0")){
				 hql = "from Componentpart componentpart where componentpart.name like '%"
							+ keywords + "%' order by componentpart.outtime asc";
			 }else{
				 hql = "from Componentpart componentpart where componentpart.name like '%"
							+ keywords + "%' order by componentpart.outtime desc";
			 }
		}else{
			if(order.equals("0")){
				hql = "from Componentpart componentpart where componentpart.name like '%"
						+ keywords + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.outtime asc";
			}else{
				hql = "from Componentpart componentpart where componentpart.name like '%"
						+ keywords + "%' and componentpart.componentnumber-"+tag+">0 and componentpart.componentnumber-"+tag+"<100 order by componentpart.outtime desc";
			}
		}
		
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<com.machine.bean.Componentpart> comList = query.list();
		return comList;
	}


	
	

	@Override
	public int update(com.machine.bean.Componentpart component) {
		sessionfactory.getCurrentSession().update(component);
		return 1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Componentpart> findAll() {
		String hql = "from Componentpart componentpart";
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<com.machine.bean.Componentpart> comList = query.list();
		return comList;
	}

	@Override
	public int deleteByIdList(List<Integer> idList) {
		Componentpart component = null;
		for (Integer id : idList) {
			component = new Componentpart();
			component.setId(id);
			sessionfactory.getCurrentSession().delete(component);
		}
		return 1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Componentpart findById(Integer id) {
		String hql = "from Componentpart componentpart where componentpart.id ="
				+ id + " ";
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<com.machine.bean.Componentpart > comList = query.list();
		if (comList.size() > 0) {
			return comList.get(0);
		}
		return null;
	}

	@Override
	public List queryForPage(String hql, int offset, int length) {
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);  
        query.setFirstResult(offset);  
        query.setMaxResults(length);  
        List list = query.list();  
		return list;
	}

	@Override
	public int getAllRowCount(String hql) {
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createQuery(hql);  
		List list = query.list();  
		System.out.println("cout : " + list.size());
		return list.size();
	}

	@Override
	public void insertNewKeywords(String keywords) {
		Session session = sessionfactory.getCurrentSession();
		String sql = "select id from searchkeywords where keywords = '"+keywords+"'";
		Query query = session.createSQLQuery(sql);
		
		List list = query.list();
		if(list.size()>=1){
			String id = String.valueOf(list.get(0));
			sql = "update searchkeywords set count = count+1 where id ="+id+"";
			query = session.createSQLQuery(sql);
	//		query.setString("myid", id);
			query.executeUpdate();
		}else{
			sql = "insert into searchkeywords(keywords,count) values('"+keywords+"',1)";
			query = session.createSQLQuery(sql);
			query.executeUpdate();
		}
	}

	@Override
	public int uploadModel(Model model) {
		sessionfactory.getCurrentSession().save(model);
		return 0;
	}

	@Override
	public int addModel(int id, String path) {
		String sql = "update componentpart set path = '"+ path + "' where id ="+id+"";
		Session session = sessionfactory.getCurrentSession();
		Query query = session.createSQLQuery(sql);
		query.executeUpdate();
		return 0;
	}

	

}
